package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.AdminTransferReqDto;
import com.yixun.qfbao.apidto.AdminTransferResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.PartnerInfoDto;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AdminTransferReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import net.sf.oval.constraint.AssertURL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 分销管理员转移
 *
 * @classname: AdminTransferHander.java
 * @author: Beau
 * @create: 2020/01/17 16:19
 * @version: 1.0.0
 **/
@Service("adminTransferHander")
public class AdminTransferHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AdminTransferHander.class);

    @Autowired
    private PartnerInfoService partnerInfoService;
    @Autowired
    private LoginUtils loginUtils;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        AdminTransferReqVo reqDto1 = (AdminTransferReqVo) reqDto;
        AdminTransferReqDto params = reqDto1.getParams();
        BaseResponseDTO<AdminTransferResDto> res = new BaseResponseDTO<>();
        AdminTransferResDto resDto = new AdminTransferResDto();
        PartnerInfo pi = loginUtils.getUser(params.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        PartnerInfo oldPi = partnerInfoService.selectById(params.getOldAdminId());
        if (!MechanismAccountTypeEnum.mechanism.getNo().equals(oldPi.getAccountType())) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("转移的用户不是管理员");
            return res;
        }
        try {
            PartnerInfoDto oldAdmin = new PartnerInfoDto();
            PartnerInfoDto newAdmin = new PartnerInfoDto();

            oldAdmin.setId(params.getNewAdminId());
            oldAdmin.setDeptId(params.getFirstDeptId());
            oldAdmin.setAccountType(MechanismAccountTypeEnum.mechanism.getNo());
            newAdmin.setId(params.getOldAdminId());
            newAdmin.setAccountType(MechanismAccountTypeEnum.individual.getNo());
            partnerInfoService.adminTransfer(oldAdmin,newAdmin);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("管理员转移成功");
        } catch (Exception e) {
            logger.error("AdminTransferHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("管理员转移失败");
        }
        res.setResult(resDto.getResult());
        res.setResData(resDto);
        return res;
    }
}
